/**
 * 根据传入颜色值，改变颜色深浅
 *
 * @param hex hex颜色字符
 * @param lum 整数0-1，正数表示颜色变浅多少，否则反之
 * @returns hex
 */
export function colorLuminance(hex: string, lum: number) {
  // validate hex string
  hex = String(hex).replace(/[^0-9a-f]/gi, '')
  if (hex.length < 6) {
    hex = hex[0] + hex[0] + hex[1] + hex[1] + hex[2] + hex[2]
  }

  lum = lum || 0

  // convert to decimal and change luminosity
  let rgb = '#',
    c,
    i

  for (i = 0; i < 3; i++) {
    c = parseInt(hex.substring(i * 2, i * 2 + 2), 16)
    c = Math.round(Math.min(Math.max(0, c + c * lum), 255)).toString(16)
    rgb += ('00' + c).substring(c.length)
  }
  return rgb
}

/**
 * 判断颜色是深色还是浅色
 *
 * @param hex hex颜色字符
 * @desc 根据传入的颜色来判断返回深色还是白色
 * @returns hex
 */
export function getContrast(hex: string) {
  const r = parseInt(hex.substring(1, 2), 16),
    g = parseInt(hex.substring(3, 2), 16),
    b = parseInt(hex.substring(5, 2), 16),
    yiq = (r * 299 + g * 587 + b * 114) / 1000
  return yiq >= 128 ? '#001f3f' : '#F6F5F7'
}

//? 以下是自定义 -----------------------------------
/**
 *
 * @param ratio 占比
 * @returns 根据占比返回颜色
 */
export function ratio2color(ratio: number, isLinear?: boolean) {
  if (ratio >= 80) {
    return '#19AD54'
  } else if (ratio >= 60) {
    return '#F9B641'
  } else {
    return '#909399'
  }
}

/**
 *
 * @param percent 百分比
 * @returns 根据百分比返回图片名称
 */
export const percent2ImgName = (percent: number): string => {
  if (percent === 0) {
    return 'arrow-success'
  } else if (percent > 0) {
    return 'arrow-up-success'
  } else {
    return 'arrow-down-danger'
  }
}

/**
 *
 * @param percent 百分比
 * @returns 根据百分比返回颜色
 */
export const percent2Color = (percent: number): string => {
  if (percent >= 0) {
    return '#19AD54'
  } else {
    return '#F25656'
  }
}

/**
 *
 * @param status 参数状态
 * @returns 根据参数状态返回颜色
 */
export const paramStatus2Color = (status: string): string => {
  switch (status) {
    case '适宜':
    case '正常':
      return '#19AD54'
    case '偏高':
      return '#F25656'
    case '偏低':
      return '#F9B641'
    case '故障':
    default:
      return '#A4A4A4'
  }
}

export function pmStatusCode2Color(code: string) {
  switch (code) {
    case '0034':
      return '#67C23A'
    case '0035':
      return '#F25656'
    default:
      return ' #eebe77'
  }
}
